//
//  FriendsViewController.m
//  shopping
//
//  Created by 马田成 on 2017/8/30.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "FriendsViewController.h"
//#import "FriendNormalTableViewCell.h"
#import "NSString+PinYin.h"
#import "MyFriendTableViewCell.h"
#import "FriendHeadView.h"

@interface FriendsViewController ()<UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate>
{

    UIView *grayView;
    
}
@property (nonatomic, strong) UITableView *tableView;
@property (strong, nonatomic) FriendHeadView *friendHeadView;
@property (nonatomic, strong) NSArray *indexArray;
@property(nonatomic,strong)  NSMutableArray  *dataArray;

@end

@implementation FriendsViewController

- (void)viewWillAppear:(BOOL)animated{

    grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
    grayView.backgroundColor = [UIColor colorWithRed:0 / 255.0f green:0 / 255.0f blue:0 / 255.0f alpha:0.2];
    
 //   [[UIApplication sharedApplication].keyWindow addSubview:grayView];
    
    [self.view addSubview:grayView];
    
    //    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView)];
    //    [grayView addGestureRecognizer:tapGesture];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"开发中，敬请期待！";
    [grayView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo (self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
        make.height.offset(20);
        
    }];


}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self leftNavHide];
    [self creationView];
    [self regeditCell];
    
    

    // Do any additional setup after loading the view.
}

- (void)creationView{

    [self setTitle:@"社交"];
    [self leftNavHide];
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), 1)];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 1, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT- 50 - 64);
    
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    _friendHeadView = [[NSBundle mainBundle] loadNibNamed:@"FriendHeadView" owner:self options:nil].lastObject;
    
    _friendHeadView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 201);
    
    self.tableView.tableHeaderView = _friendHeadView;
    
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //   self.tableView.estimatedRowHeight = 400;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    self.tableView.sectionIndexColor = getColor(@"666666");

    [self.view addSubview:self.tableView];
    UIButton *buttonAdd = [[UIButton alloc] init];
    
    buttonAdd.frame = CGRectMake(DEF_SCREEN_WIDTH - 50, 0, 40, 40);
    
    [buttonAdd setImage:[UIImage imageNamed:@"haoyou_icon_xiaoxi-"] forState:UIControlStateNormal];
    [buttonAdd addTarget:self action:@selector(messageClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:buttonAdd];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, screenScale(20), screenScale(20));
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"haoyou_icon_sousuo"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    
    
    
    NSArray *array = @[@"登记", @"大奔", @"周傅", @"爱德华",@"((((", @"啦文琪羊", @"   s文强", @"过段时间", @"等等", @"各个", @"宵夜**", @"***", @"贝尔",@"*而结婚*", @"返回***", @"你还", @"与非门*", @"是的", @"*模块*", @"*没做*",@"俄文", @"   *#咳嗽", @"6",@"fh",@"C罗",@"邓肯"];
    
    self.indexArray = [array arrayWithPinYinFirstLetterFormat];
    self.dataArray =[NSMutableArray arrayWithArray:_indexArray];
    
    [self.tableView reloadData];
    
    
    
    
}
- (void)regeditCell{
    
//    [self.tableView registerNib:[UINib nibWithNibName:@"FriendNormalTableViewCell" bundle:nil] forCellReuseIdentifier:@"FriendNormalTableViewCell"];
   [self.tableView registerNib:[UINib nibWithNibName:@"MyFriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyFriendTableViewCell"];
    
}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.dataArray count];
    
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 1;
    }else{
        NSDictionary *dict = self.dataArray[section];
        NSMutableArray *array = dict[@"content"];
        return [array count];
    }
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    if (indexPath.section == 0) {
//        return 211;
//    }else {
//    
//        return 64;
//        
//    }
    return 64;
    
}

#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
//        
//        FriendNormalTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"FriendNormalTableViewCell" forIndexPath:indexPath];
//        cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        return cell;
//        
//    }else {
//    
//    }
    
    MyFriendTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyFriendTableViewCell"];
    
    NSDictionary *dict = self.dataArray[indexPath.section];
    NSMutableArray *array = dict[@"content"];
    cell.nameLabel.text = array[indexPath.row];

    return cell;
    
    
    
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //自定义Header标题
    UIView* myView = [[UIView alloc] init];
    myView.backgroundColor = getColor(@"DFDFDF");
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
    titleLabel.textColor=getColor(@"666666");
    
    NSString *title = self.dataArray[section][@"firstLetter"];
    titleLabel.text=title;
    [myView  addSubview:titleLabel];
    
    return myView;
}
#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    
}
#pragma mark - ---------- 右边消息 ----------

- (void)messageClick{

    
}
#pragma mark - ---------- 左边搜索消息 ----------
- (void)searchClick{

    
}
#pragma mark---tableView索引相关设置----
//添加TableView头视图标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dict = self.dataArray[section];
    NSString *title = dict[@"firstLetter"];
    return title;
}


//添加索引栏标题数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *resultArray =[NSMutableArray array];
    
    for (NSDictionary *dict in self.dataArray) {
        NSString *title = dict[@"firstLetter"];
        [resultArray addObject:title];
    }
    return resultArray;
}


//点击索引栏标题时执行
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //这里是为了指定索引index对应的是哪个section的，默认的话直接返回index就好。其他需要定制的就针对性处理

    if ([title isEqualToString:UITableViewIndexSearch])
    {
        [tableView setContentOffset:CGPointZero animated:NO];//tabview移至顶部
        return NSNotFound;
    }
    else
    {
        return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index] - 1; // -1 添加了搜索标识
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
